package com.banciyuan.bcywebview.biz.write.photoselecotor.ui;
/**
 * @author Aizaz AZ
 */

import android.os.Bundle;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.write.photoselecotor.model.PhotoModel;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.biz.write.photoselecotor.pselector.PhotoHelper;
import com.banciyuan.bcywebview.biz.write.photoselecotor.pselector.PhotoListener;

import java.util.ArrayList;
import java.util.List;


public class PhotoPreviewActivity extends BasePhotoPreviewActivity implements PhotoListener {

    private PhotoHelper photoHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        photoHelper = new PhotoHelper(getApplicationContext());
        init(getIntent().getExtras());
    }

    @SuppressWarnings("unchecked")
    protected void init(Bundle extras) {
        try {
            if (extras == null) return;
            if (extras.containsKey("photos")) { // 预览图片
                photos = (ArrayList<PhotoModel>) extras.getSerializable("photos");
                selectedPhotos.clear();
                selectedPhotos.addAll(photos);
                current = extras.getInt("position", 0);
                updatePercent();
                bindData();
            } else if (extras.containsKey("album")) { // 点击图片查看
                String albumName = extras.getString("album"); // 相册
                this.current = extras.getInt("position");
                selectedPhotos = (ArrayList<PhotoModel>) extras.getSerializable("selectedphotos");
                if (!CommonUtils.isNull(albumName) && albumName.equals(getString(R.string.recent_photos))) {
                    photoHelper.getReccent(this);
                } else {
                    photoHelper.getAlbum(albumName, this);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPhotoLoaded(List<PhotoModel> photos) {
        this.photos = photos;
        for (PhotoModel model : photos) {
            if (selectedPhotos.contains(model)) {
                model.setChecked(true);
            }
        }
        updatePercent();
        bindData(); // 更新界面
    }

}
